﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MVCBugTracker.Models
{
    public class EFProjectRepository : IProjectRepository
    {
        MVCBUGTRACKEREntities db = new MVCBUGTRACKEREntities();

        public IQueryable<Project> GetAllProjects()
        {
            return from p in db.Projects
                   where p.Deleted == false
                   select p;
        }

        public IQueryable<Project> GetMyProjects(string userName)
        {
            return from p in db.Projects
                   join m in db.ProjectMembers on p.ProjectID equals m.ProjectID
                   where m.UserName == userName
                   && p.Deleted == false
                   select p;
        }

        public Project GetProject(int projectID)
        {
            return db.Projects.SingleOrDefault(p => p.ProjectID == projectID);
        }

        public void AddProject(Project project)
        {
            db.Projects.AddObject(project);
        }

        public void DeleteProject(Project project)
        {
            project.Deleted = true;
        }

        public void Save()
        {
            db.SaveChanges();
        }

        public IQueryable<ProjectMilestone> GetProjectMilestones(int projectID)
        {
            return from m in db.ProjectMilestones
                   where m.ProjectID == projectID
                   select m;
        }

        public ProjectMilestone GetProjectMilestone(int milestoneID)
        {
            return db.ProjectMilestones.SingleOrDefault(m => m.MilestoneID == milestoneID);
        }

        public void AddProjectMilestone(ProjectMilestone milestone)
        {
            db.ProjectMilestones.AddObject(milestone);
        }

        public void DeleteProjectMilestone(ProjectMilestone milestone)
        {
            var bugs = from b in db.Bugs
                       where b.ProjectID == milestone.ProjectID
                       && b.MilestoneID == milestone.MilestoneID
                       select b;
            foreach (Bug b in bugs)
            {
                b.MilestoneID = null;
            }
            db.ProjectMilestones.DeleteObject(milestone);
        }

        public IQueryable<ProjectCategory> GetProjectCategories(int projectID)
        {
            return from c in db.ProjectCategories
                   where c.ProjectID == projectID
                   select c;
        }

        public ProjectCategory GetProjectCategory(int categoryID)
        {
            return db.ProjectCategories.SingleOrDefault(c => c.CategoryID == categoryID);
        }

        public void AddProjectCategory(ProjectCategory category)
        {
            db.ProjectCategories.AddObject(category);
        }

        public void DeleteProjectCategory(ProjectCategory category)
        {
            var bugs = from b in db.Bugs
                       where b.ProjectID == category.ProjectID
                       && b.CategoryID == category.CategoryID
                       select b;
            foreach (Bug b in bugs)
            {
                b.CategoryID = null;
            }
            db.ProjectCategories.DeleteObject(category);
        }

        public IQueryable<ProjectPriority> GetProjectPriorities(int projectID)
        {
            return from p in db.ProjectPriorities
                   where p.ProjectID == projectID
                   select p;
        }

        public ProjectPriority GetProjectPriority(int priorityID)
        {
            return db.ProjectPriorities.SingleOrDefault(p => p.PriorityID == priorityID);
        }

        public void AddProjectPriority(ProjectPriority priority)
        {
            db.ProjectPriorities.AddObject(priority);
        }

        public void DeleteProjectPriority(ProjectPriority priority)
        {
            var bugs = from b in db.Bugs
                       where b.ProjectID == priority.ProjectID
                       && b.PriorityID == priority.PriorityID
                       select b;
            foreach (Bug b in bugs)
            {
                b.PriorityID = null;
            }
            db.ProjectPriorities.DeleteObject(priority);
        }

        public IQueryable<ProjectStatus> GetProjectStatuses(int projectID)
        {
            return from s in db.ProjectStatuses
                   where s.ProjectID == projectID
                   select s;
        }

        public ProjectStatus GetProjectStatus(int statusID)
        {
            return db.ProjectStatuses.SingleOrDefault(s => s.StatusID == statusID);
        }

        public void AddProjectStatus(ProjectStatus status)
        {
            db.ProjectStatuses.AddObject(status);
        }

        public void DeleteProjectStatus(ProjectStatus status)
        {
            var bugs = from b in db.Bugs
                       where b.ProjectID == status.ProjectID
                       && b.StatusID == status.StatusID
                       select b;
            foreach (Bug b in bugs)
            {
                b.StatusID = null;
            }
            db.ProjectStatuses.DeleteObject(status);
        }

        public IQueryable<ProjectCustomField> GetProjectCustomFields(int projectID)
        {
            return from cf in db.ProjectCustomFields
                   where cf.ProjectID == projectID
                   && cf.Deleted == false
                   select cf;
        }

        public ProjectCustomField GetProjectCustomField(int customFieldID)
        {
            return db.ProjectCustomFields.SingleOrDefault(cf => cf.CustomFieldID == customFieldID);
        }

        public void AddProjectCustomField(ProjectCustomField customField)
        {
            db.ProjectCustomFields.AddObject(customField);
        }

        public void DeleteProjectCustomField(ProjectCustomField customField)
        {
            customField.Deleted = true;
        }

        public IQueryable<ProjectCustomFieldValue> GetProjectCustomFieldValues(int projectID)
        {
            throw new NotImplementedException();
        }

        public ProjectCustomFieldValue GetProjectCustomFieldValue(int customFieldID)
        {
            throw new NotImplementedException();
        }

        public void AddProjectCustomFieldValue(ProjectCustomFieldValue customFieldValue)
        {
            throw new NotImplementedException();
        }

        public void DeleteProjectCustomFieldValue(ProjectCustomFieldValue customFieldValue)
        {
            throw new NotImplementedException();
        }

        public IQueryable<ProjectMember> GetProjectMembers(int projectID)
        {
            return from m in db.ProjectMembers
                   where m.ProjectID == projectID
                   orderby m.UserName ascending
                   select m;
        }

        public ProjectMember GetProjectMember(int projectID, string userName)
        {
            return (from m in db.ProjectMembers
                    where m.ProjectID == projectID
                    && m.UserName == userName
                    select m).SingleOrDefault();
        }

        public void AddProjectMember(ProjectMember projectMember)
        {
            db.ProjectMembers.AddObject(projectMember);
        }

        public void DeleteProjectMember(ProjectMember projectMember)
        {
            db.ProjectMembers.DeleteObject(projectMember);
        }
    }
}